import { HttpLink } from 'apollo-link-http'
import { ApolloLink } from 'apollo-link'
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import store from './store'

const uri = 'http://localhost:60000/simple/v1/cje9zdzfe00040176r5kfajvj'

const middleware = new ApolloLink((operation, forward) => {
  const { token } = store.authentication
  const headers = token ? { authorization: `Bearer ${token}` } : {}
  operation.setContext({ headers })
  return forward(operation)
})

export default new ApolloClient({
  link: middleware.concat(new HttpLink({ uri })),
  cache: new InMemoryCache(),
})
